import os

import requests
import zipfile
from loguru import logger


def download_file(url, filename):
    # 发送 GET 请求
    response = requests.get(url, stream=True)

    # 确保请求成功
    response.raise_for_status()

    # 打开文件，以二进制写入模式
    with open(filename, 'wb') as file:
        # 迭代响应内容，分块写入文件
        for chunk in response.iter_content(chunk_size=8192):
            file.write(chunk)


def is_directory(path):
    return os.path.isdir(path)


def create_folder_if_not_exists(folder_path):
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
        logger.info(f"Folder '{folder_path}' created successfully.")
    else:
        logger.info(f"Folder '{folder_path}' already exists.")


def unzip_file(zip_file_path, extract_dir):
    with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
        zip_ref.extractall(extract_dir)
